<?php
/**
 * +--------------------------------------------------------------
 * | eLuoPHP
 * +--------------------------------------------------------------
 * | Copyright (c) 2016-2018 http://qxwoo.cn All rights reserved.
 * +--------------------------------------------------------------
 * | y139w [y139w@163.com]
 * +--------------------------------------------------------------
 * | 加密工具类文件
 * +--------------------------------------------------------------
 */
namespace luo\traits;

trait Crypts
{

    private static $count = 2;

    private static $cryptKey = 'http://qxwoo.cn';

    /**
     * BASE64加密
     * @param $data
     * @return string
     */
    public static function base64encode($data){

        $string = serialize($data);
        $base64 = base64_encode($string);
        $key = base64_encode(md5(self::$cryptKey));
        $string = base64_encode($base64 . $key);
        $len = strlen($string);
        if(($len % 2) == 0){
            $num = $len / 2;
            $string = substr($string, $num, $len - 1) . substr($string, 0, $num);
        }
        return base64_encode($string);

    }

    /**
     * BASE64解密
     * @param $data
     * @return mixed
     */
    public static function base64decode($data){

        if(is_string($data)){
            $string = base64_decode($data);
            $len = strlen($string);
            if(($len % 2) == 0){
                $num = $len / 2;
                $string = substr($string, $num, $len - 1) . substr($string, 0, $num);
            }
            $string = base64_decode($string);
            $key = base64_encode(md5(self::$cryptKey));
            $string = str_replace($key, '', $string);
            $string = base64_decode($string);
            return unserialize($string);
        }
        return $data;

    }

    /**
     * md5加密加强
     * @param $data
     * @param bool $lowerCase
     * @return string
     */
    public static function md5($data, $lowerCase = true){

        $data   = serialize($data);
        $string = md5($data);
        $left   = substr($string, 0, strlen($string) / 2);
        $right  = substr($string, (strlen($string) / 2 - 1), strlen($string) / 2);
        $string = md5($right . $string . $left);
        $key    = md5(self::$cryptKey);
        $string = md5($string . $key);
        $string = md5($string);
        if(!$lowerCase){
            $string = strtoupper($string);
        }
        return $string;

    }

    /**
     * openssl对称加密
     * @param $info
     * @return string
     */
    public static function encrypt($info){

        // 实例化要加密的数据
        $info    = serialize($info);
        // 加密KEY
        $key     = md5(self::$cryptKey);
        // 非 NULL 的初始化向量
        $iv      = base64_encode(substr($key,0,strlen($key) / 2));
        // openssl_encrypt 加密
        $data    = openssl_encrypt($info, 'AES-256-CBC', $key, 0, base64_decode($iv));
        $encrypt = base64_encode(json_encode($data));
        return $encrypt;

    }

    /**
     * openssl对称解密
     * @param $encrypt
     * @return mixed
     */
    public static function decrypt($encrypt){

        // 加密KEY
        $key     = md5(self::$cryptKey);
        // 还原
        $encrypt = json_decode(base64_decode($encrypt), true);
        // 非 NULL 的初始化向量
        $iv      = base64_encode(substr($key,0,strlen($key) / 2));
        // 解密
        $decrypt = openssl_decrypt($encrypt, 'AES-256-CBC', $key, 0, base64_decode($iv));
        $data    = unserialize($decrypt);
        return $data;

    }
}